package com.etc.util;

import com.etc.dao.MovieDao;
import com.etc.entity.Movie;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
@EnableScheduling
public class MovieReptile {
    @Autowired
    private MovieDao movieDao;


    @Scheduled(fixedDelay = 3600000)
    public void reptileMovie() throws IOException {

        String source = "https://dianying.taobao.com/showList.htm?spm=a1z21.3046609.header.4.32c0112ayhfFrK&n_s=new";
        Document doc = Jsoup.connect(source).get();

        Elements links = doc.select("div[class=\"movie-card-wrap\"]");
        for (Element element : links) {// 一个element是一部电影

            //System.out.println(element);
            String mpicture = null;// 电影封面
            int mstatus = 1;// 电影状态 ： 热映中1 即将上映 2
            String mname = null;// 电影名字
            String mgrade = null;//电影评分
            String mdirector = null;// 电影导演
            String mactor = null;// 电影主演
            String mtype = null;// 电影类型
            String mcountry = null;// 电影地区
            String mlanguage = null;// 电影语言
            String mplaytime = null;// 电影片长
            String mreleasedtime = "选座购票	";// 上映时间
            /*
             * 爬取封面
             */
            Elements jpgs = element.select("img[src$=.jpg]");// 正在上映的电影 封面有 data-src
            for (Element elementJpg : jpgs) {
                mpicture = elementJpg.attr("src").replace("160x240", "320x480");
            }
            /*
             * 获取电影名
             */
            Elements names = element.select("span[class$=bt-l]");
            for (Element elementName : names) {
                mname = elementName.text();
            }
            /*
             * 获取电影评分
             */
            Elements grades = element.select("span[class$=bt-r]");
            for (Element elemenGrade : grades) {
                mgrade = elemenGrade.text();
            }
            /*
             * 获取电影其他信息
             */
            Elements others = element.select("span");
            for (Element info : others) {
                if (info.text().contains("导演")) {
                    mdirector = info.text();
                }
                if (info.text().contains("主演")) {
                    mactor = info.text();
                }
                if (info.text().contains("类型")) {
                    mtype = info.text();
                }
                if (info.text().contains("地区")) {
                    mcountry = info.text();
                }
                if (info.text().contains("语言")) {
                    mlanguage = info.text();
                }
                if (info.text().contains("片长")) {
                    mplaytime = info.text();
                }
            }

            /*
             * 获取电影上映时间
             */
            Elements released = element.select("a[class=\"movie-card-soon\"]");
            for (Element releasedtime : released) {
                if (releasedtime.html() != null) {
                    mreleasedtime = releasedtime.html();
                    mstatus = 2;
                }
            }
            Movie m = new Movie(mname, mgrade, mpicture, mstatus, mdirector, mactor, mtype, mcountry, mlanguage, mplaytime, mreleasedtime);
            System.out.println(mname);

            if (movieDao.findByMname(mname)!=null && !movieDao.findByMname(mname).getMreleasedtime().trim().equals( mreleasedtime.trim())){
                //① 如果爬下的电影名在数据库里存在 ② 通过电影名在数据库查找到的 movie对象的 状态 和 爬下来的状态不一样
                m.setMid(movieDao.findByMname(mname).getMid());
                movieDao.save(m);
            }
            if (movieDao.findByMname(mname) == null && mplaytime != null ) {
                // ①爬下的电影名 在数据库中查找不到 ② 爬下的电影时长不是空
                Pattern pattern = Pattern.compile("\\d{2,3}");
                Matcher matcher = pattern.matcher(mplaytime);

                if (matcher.find()) {

                    movieDao.save(m);

                }
            }
            //System.out.println("************************************88");

        }

    }
}
